package com.xiaohuang.dao;

import com.xiaohuang.pojo.Department;
import com.xiaohuang.pojo.Employee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
@Repository
public class EmployeeDao {
    //模拟数据库中的数据
    private static Map<Integer, Employee> employees = null;
    @Autowired
    private DepartmentDao departmentDao;
    // 静态代码块是优先于main方法执行的，所以在这里初始化，保证在main方法执行前，数据已经初始化，否则会报空指针异常。
    static {
        employees = new HashMap<Integer, Employee>(); //创建部门表
        employees.put(1001,new Employee(1001,"AA","AA@163.com",1,new Department(101,"教学部")));
        employees.put(1002,new Employee(1002,"BB","BB@163.com",1,new Department(102,"市场部")));
        employees.put(1003,new Employee(1003,"CC","CC@163.com",1,new Department(103,"教研部")));
        employees.put(1004,new Employee(1004,"DD","DD@163.com",0,new Department(104,"运营部")));
        employees.put(1005,new Employee(1005,"EE","EE@163.com",1,new Department(105,"后勤部")));
    }
    //主键自增
    private static Integer initId = 1006;
    //添加员工
    public void add(Employee employee){
        if (employee.getId() == null){
            employee.setId(initId++);
        }
        employee.setDepartment(departmentDao.getDepartmentById(employee.getDepartment().getId()));
        employees.put(employee.getId(),employee);
    }

    //查询所有员工
    public Collection<Employee> getAll(){
        return employees.values();
    }
    //通过id查询员工
    public Employee getEmployeeById(Integer id){
        return employees.get(id);
    }
    //删除员工
    public void delete(Integer id){
        employees.remove(id);
    }

}
